﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace ToDoList.Logic
{
    public class InventoryService
    {
        // 创建一个清单
        public void CreateInventory(int userID, string inventoryName)
        {
            User user = UserService.FindUser(userID);
            Inventory inventory = new Inventory();
            inventory.InventoryName = inventoryName;
            inventory.UserID = userID;
            inventory.InventoryID = user.Inventories.Count + 1;
            inventory.DateOfInventoryCreated = DateTime.Now;
            
            user.Inventories.Add(inventory);
            Data.Inventories.Add(inventory);
        }

        // 添加事项
        public Matter AddMatter(int userID, int inventoryID, string title)
        {
            try
            {
                User user = UserService.FindUser(userID);
                Inventory inventory = FindInventory(userID, inventoryID);
                Matter matter = new Matter()
                {
                    MatterTitle = title,
                    MatterID = inventory.Matters.Count + 1,
                    InventoryID = inventoryID,
                    UserID = userID,
                    DateOfMatterCreated = DateTime.Now
                };
                Data.Matters.Add(matter);
                user.Matters.Add(matter);
                inventory.Matters.Add(matter);
                return matter;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }

        }

        // 删除一个事项
        public void RomoveAMatter(int userID, int inventoryID, int matterID)
        {
            User user = UserService.FindUser(userID);
            if (!MatterService.IsThisMatterExisted(userID, inventoryID, matterID))
                throw new Exception("This matter have not been existed");
            else
            {
                // 找到对应的清单
                var thisUserList = FindInventory(userID, inventoryID);
                // 找到这个事项
                var matter = MatterService.FindMatter(userID, inventoryID, matterID);
                Data.Matters.Remove(matter);
                user.Matters.Remove(matter);
                thisUserList.Matters.Remove(matter);
            }
        }

        // 设置清单名称
        public void SetEventsListName(int userID, int inventoryID, string name)
        {
            try
            {
                FindInventory(userID, inventoryID).InventoryName = name;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        // 找到指定的清单
        public static Inventory FindInventory(int userID, int inventoryID)
        {
            var user = UserService.FindUser(userID);
            var inventory = user.Inventories.SingleOrDefault(i => i.InventoryID == inventoryID);
            if (inventory == null)
                throw new Exception("This inventory may not exist");
            else
                return inventory;
        }

        // 指定的清单是否存在
        public static bool IsThisInventoryExisted(int userID, int inventoryID)
        {
            return Data.Users.Find(user => user.UserID == userID).Inventories.
                Any(inventory => inventory.InventoryID == inventoryID);
        }
    }
}
